package com.chub.study.modules.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chub.RespResult;
import com.chub.study.util.SmartWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.chub.study.modules.service.TMessageService;
import com.chub.study.modules.entity.TMessage;


/**
 * <p>
 * 报文表（请求报文，响应报文）
 * </p>
 *
 * @author Chub
 * @since 2022-11-02
 */
@Api(tags = "报文表（请求报文，响应报文）服务")
@RestController
@RequestMapping("/tmessage")
public class TMessageController {

    @Resource
    private TMessageService tMessageService;

    @ApiOperation("创建")
    @PostMapping("/create")
    public RespResult<String> create(@Valid @RequestBody TMessage tMessage) {
        tMessageService.save(tMessage);
        return RespResult.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public RespResult<String> delete(@RequestParam String id) {
        tMessageService.removeById(id);
        return RespResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/modify")
    public RespResult<String> modify(@Valid @RequestBody TMessage tMessage) {
        tMessageService.updateById(tMessage);
        return RespResult.success();
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/detail")
    public RespResult<TMessage> detail(@RequestParam String id) {
        return RespResult.success(tMessageService.getById(id));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/queryByPage")
    public RespResult<IPage<TMessage>> queryByPage(@RequestBody JSONObject queryObject) {

         SmartWrapper<TMessage> smartWrapper = new SmartWrapper<>();
         smartWrapper.parse(queryObject, TMessage.class);

         QueryWrapper<TMessage> queryWrapper = smartWrapper.getQueryWrapper();
         IPage<TMessage> page = smartWrapper.generatePage();
         tMessageService.page(page, queryWrapper);
         return RespResult.success(page);
    }

}